﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CP2011_Assignment
{
    public partial class CreateInvUI : Form
    {
        

        public CreateInvUI()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtInventoryName.TextLength > 0 && txtDescription.TextLength > 0 && txtMaxQuant.TextLength > 0 && txtSupplier.TextLength > 0)
            { 
                addingRecord(new InventorySystem());
                
            }
            else
            {
                MessageBox.Show("Please enter value for all the fields", "Fields Empty", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Show();
            }
            
           
            
        }

        /// <summary>
        /// to preserve Dependency Inversion Principle
        /// </summary>
        /// <param name="dataCtrl"></param>
        private void addingRecord(IDataController dataCtrl)
        {
            ///create new inventory object 
            try
            {
                Inventory inv = new Inventory(txtInventoryName.Text, txtDescription.Text, Convert.ToInt32(txtMaxQuant.Text), Convert.ToInt32(txtSupplier.Text));
                dataCtrl.addToDatabase(inv);
                MessageBox.Show("New Item Successfuly Added!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Hide();
                this.Owner.Show();
                this.Dispose();
            }
            catch
            {
                MessageBox.Show("Input format is incorrect, please insert only integer value SUPPLIER and QUANTITY PER MONTH", "Incorrect Input Format", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Show();
            }
        }

        private void CreateInvUI_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            this.Owner.Show();
            this.Dispose();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Owner.Show();
            this.Dispose();
        }

       
    }
}
